package com.xyh.student.service.impl;

import com.xyh.student.dao.StudentDao;
import com.xyh.student.entity.ResponseData;
import com.xyh.student.entity.Student;
import com.xyh.student.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;


@Service("studentService")
public class StudentServiceImpl implements StudentService {

    @Autowired
    private StudentDao studentDao;

    @Override
    public ResponseData queryAllStudents(Integer pageNum, Integer pageSize){
        try{
            List<Student> student=this.studentDao.queryAllStudents(pageNum,pageSize);
            return new ResponseData("0","请求成功",student);
        }catch (Exception e){
            System.out.println(e);
            return new ResponseData("888","请求失败");
        }

    }

    @Override
    public ResponseData queryByName(String name) {

        try{
            name = "%"+name+"%";
            List<Student> student= this.studentDao.queryByName(name);
            return new ResponseData("0","请求成功",student);
        }catch (Exception e){
            System.out.println(e);
            return new ResponseData("888","请求失败");
        }
    }
    @Override
    public ResponseData queryBySid(long sid) {

        try{
            List<Student> student= this.studentDao.queryBySid(sid);
            return new ResponseData("0","请求成功",student);
        }catch (Exception e){
            System.out.println(e);
            return new ResponseData("888","请求失败");
        }
    }

    @Override
    public ResponseData insertStudent(Student student) {

        try{
            int a=this.studentDao.insertStudent(student);
            return new ResponseData("0","请求成功",a);
        }catch (Exception e){
            System.out.println(e);
            return new ResponseData("888","请求失败");
        }

    }

    @Override
    public ResponseData deleteStudentBySid(long sid) {
        try{
            int d= this.studentDao.deleteStudentBySid(sid);
            return new ResponseData("0","请求成功",d);
        }catch (Exception e){
            System.out.println(e);
            return new ResponseData("888","请求失败");
        }

    }

    @Override
    public ResponseData deletes(long[] sids) {

        try{
            int d=this.studentDao.deletes(sids);
            return new ResponseData("0","请求成功",d);
        }catch (Exception e){
            System.out.println(e);
            return new ResponseData("888","请求失败");
        }
    }

    @Override
    public ResponseData updateStudent(Student student) {

        try{
            int u= this.studentDao.updateStudent(student);
            return new ResponseData("0","请求成功",u);
        }catch (Exception e){
            System.out.println(e);
            return new ResponseData("888","请求失败");
        }
    }

    @Override
    public ResponseData numberOfStudents() {

        try{
            int n= this.studentDao.numberOfStudents();
            return new ResponseData("0","请求成功",n);
        }catch (Exception e){
            System.out.println(e);
            return new ResponseData("888","请求失败");
        }

    }

    @Override
    public ResponseData queryCidByName(String cname) {
        try{
            int cid=this.studentDao.queryCidByName(cname);
            return new ResponseData("0","请求成功",cid);
        }catch (Exception e){
            System.out.println(e);
            return new ResponseData("888","请求失败");
        }
    }

    @Override
    public ResponseData InsertStudentOfCourse(Student student,String cname) {

        try{
            int cid=this.studentDao.queryCidByName(cname);
            student.setCid(cid);
            int i=this.studentDao.insertStudent(student);
            return new ResponseData("0","请求成功",i);
        }catch (Exception e){
            System.out.println(e);
            return new ResponseData("888","请求失败");
        }

    }



}
